import { request } from "../../utils/request.js";
import regeneratorRuntime from "../../utils/runtime.js";
var bsurl = require('../../utils/bsurl.js');
let app =  getApp();
Page({
    data: {
        day:0,
        tabs:[
            {
              id:0,
              value:"个性推荐",
              isActive:true
            },
            {
              id:1,
              value:"歌单",
              isActive:false
            },
            {
              id:2,
              value:"主播电台",
              isActive:false
            },
            {
              id:3,
              value:"排行榜",
              isActive:false
            }
          ],
        //轮播图数据
        swiper_item:[
            '../../icon/index/cm2_discover_banner_cn-ip6@2x.jpg',
            '../../icon/index/cm2_discover_banner_jp-ip6@2x.jpg',
            '../../icon/index/cm2_discover_banner_kr-ip6@2x.jpg',
            '../../icon/index/cm2_discover_banner_us-ip6@2x.jpg'
        ],
        personalizedlist:[],
        newsongs:[],
        djprogram:[],
        mv:[],
        isplayer:false,
        isplay:false,
    },
    //获取个性推荐
    async getRecommend(){
      const resultPersonalized=await request({url: bsurl+'personalized?limit=6'});
      const personalizedlist=resultPersonalized.result.map(v=>{
        return {
          id:v.id,
          name:v.name,
          picUrl:v.picUrl,
          playCount:v.playCount
        };
      });
      //新歌
      const resultNewsongs=await request({url: bsurl+'personalized/newsong'});
      const newsongs=resultNewsongs.result.splice(4).map(v=>{
        return {
          id:v.id,
          name:v.name,
          picUrl:v.picUrl,
        };
      });
      //主播电台
      const resultDjprogram=await request({url: bsurl+'personalized/djprogram'});
      const djprogram=resultDjprogram.result.map(v=>{
        return {
          id:v.id,
          name:v.name,
          picUrl:v.picUrl,
        };
      });
      //MV
      const resultMv=await request({url:bsurl+'personalized/mv'});
      const mv=resultMv.result.map(v=>{
        return {
          id:v.id,
          name:v.name,
          picUrl:v.picUrl,
        };
      });
      this.setData({
        personalizedlist,
        newsongs,
        djprogram,
        mv
      })
    }, 
    handleTabsItemChange(e){
      const {index}=e.detail;
      this.setTabs(index)
    },
    hotTop(){
      let index=3;
      this.setTabs(index)
    },
    //修改数组所用
    setTabs(index){
      let {tabs}=this.data;
      tabs.forEach((v,i) => i===index?v.isActive=true:v.isActive=false);
      this.setData({
      tabs
      })
    },
    //获取当天日期
    getDay(){
      var myDate = new Date();
      let day=myDate.getDate();
      this.setData({
        day
      })
      wx.setStorageSync("dat", day);  
    },
    //每日推荐歌曲
    async Commendsongs(){
    const Commendsongs=await request({url:'http://musicapi.leanapp.cn/recommend/songs'});
    wx.setStorageSync('Commendsongs', Commendsongs);
    }, 
    onLoad: function(options) {
      this.getRecommend();
      this.getDay();
      this.Commendsongs();
    },
    onShow: function() {
      this.setData({
        isplay:app.globalData.zzPlay,
        isplayer:app.globalData.playerData.isplayer
      })
    },
    onReachBottom: function() {
      let isActive=this.data.tabs[1].isActive;
      if(isActive){
        this.selectComponent("#Platlist").hanlegetplaylist()
      }
    }
});
  